package com.asiainfo.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.asiainfo.bean.HelperBean;
import com.asiainfo.bean.UserSessionBean;
import com.asiainfo.dao.BaseDao;
import com.asiainfo.util.SessionInfoUtil;


@Controller
@RequestMapping("/helperController")
public class HelperController extends ModelAndView{
	
private Logger logger = LogManager.getLogger(this.getClass());

	/**
	 * 数据操作封装DAO
	 */
	@Autowired
	private BaseDao dao;
	

	/**
	 * 方法描述：跳转到套餐余量页面
	 * 
	 * return 套餐余量页面Model
	 */
	@RequestMapping("mealAllowanceView.do")
	public ModelAndView index() {
		this.setViewName("helper/mealAllowance");
			
	
		return this;	
		}
	
	
	/**
	 * 方法描述：查询套餐余量操作
	 * 
	 */
	@RequestMapping(value = "mealAllowance.do")
	 
	public @ResponseBody
	JSONObject mealAllowance(@RequestBody HelperBean helperBean, 
			HttpServletRequest request) throws Exception {
		JSONObject retMsg = new JSONObject();
		
		UserSessionBean userSessionBean = SessionInfoUtil.getUserSessBean(request.getSession());
		
		String userPhone = userSessionBean.getPhone();
		
		//套餐总量
		int mealcall = 0;
		int mealmessage = 0;
		int mealdata = 0;
		
		//业务总量
		int businesscall = 0;
		int businessmessage = 0;
		int businessdata = 0;
		
		//使用量
		int data_use;
		int	message_use;
		int	call_use;
		
		if(userPhone !=null){
			//判断是否使用套餐
			int isMeal = (Integer)this.dao.getDAO().selectOne(
					"HelperMapper.isMyMeal", helperBean.getPhone());
			
			//判断是否使用业务
			int isBusiness = (Integer)this.dao.getDAO().selectOne(
					"HelperMapper.isMyBusiness", helperBean.getPhone());
            
			if(isMeal > 1 ){//套餐>1个
				retMsg.put("isNotOne", true);
				
			}else if(isMeal== 1||isBusiness!=0){
				
				if(isMeal== 1){
				//获得套餐总量
				HelperBean totalMeal = dao.getDAO().selectOne(
						"HelperMapper.queryTotalMeal", helperBean.getPhone());
				
				mealcall = Integer.parseInt(totalMeal.getTotalvoice());
				mealmessage = Integer.parseInt(totalMeal.getTotalmessage());
				mealdata = Integer.parseInt(totalMeal.getTotaldata());
				}
				
				if(isBusiness!=0){
				//获得业务总量
				HelperBean totalBusiness = dao.getDAO().selectOne(
						"HelperMapper.queryTotalBusiness", helperBean.getPhone());
				
				businesscall = Integer.parseInt(totalBusiness.getBustotalvoice());
				 businessmessage = Integer.parseInt(totalBusiness.getBustotalmessage());
     			 businessdata = Integer.parseInt(totalBusiness.getBustotaldata());
				}
				int totalVoice=businesscall+mealcall;
				int totalMessage=businessmessage+mealmessage;
				int totaldata=businessdata+mealdata;
				
				this.addObject("totalVoice", totalVoice);
     			this.addObject("totalMessage", totalMessage);
     			this.addObject("totaldata", totaldata);
     			 
     			//获取套餐使用量
     			HelperBean mealAllowanceInfo = dao.getDAO().selectOne(
     					"HelperMapper.queryMealAllowance", helperBean.getPhone());
     			
     			if(mealAllowanceInfo != null){
     				data_use = Integer.parseInt(mealAllowanceInfo.getData_use());
     				message_use = Integer.parseInt(mealAllowanceInfo.getMessage_use());
     				call_use = Integer.parseInt(mealAllowanceInfo.getCall_use());
     				
     			}else{
     				data_use=0;
     				message_use=0;
     				call_use=0;
     			}

 				this.addObject("data_use",data_use);
 				this.addObject("message_use",message_use);
 				this.addObject("call_use",call_use);
 				
 				int dataRemainder=totaldata-data_use;
 				int messageRemainder=totalMessage-message_use;
 				int callRemainder=totalVoice-call_use;
 				if(dataRemainder<0){
 					dataRemainder=0;
 				}
 				if(messageRemainder<0){
 					messageRemainder=0;
 				}
 				if(callRemainder<0){
 					callRemainder=0;
 				}
 				//套餐余量
 				this.addObject("dataRemainder",dataRemainder);
 				this.addObject("messageRemainder",messageRemainder);
 				this.addObject("callRemainder",callRemainder);
     			
 				//使用百分比
 				int dataPer=(data_use*100)/totaldata;
 				int messagePer=(message_use*100)/totalMessage;
 				int callPer=(call_use*100)/totalVoice;
 				if(dataPer>100){
 					dataPer=100;
 				}
 				if(messagePer>100){
 					messagePer=100;
 				}
 				if(callPer>100){
 					callPer=100;
 				}
 				this.addObject("dataPer",dataPer);
 				this.addObject("messagePer",messagePer);
 				this.addObject("callPer",callPer);
     			retMsg.put("flag", true);
			
			}else if(isMeal==0&&isBusiness==0){   //没有套餐或业务
				
				retMsg.put("isNot", true);
				
			}
			
			
		}
		else{
			retMsg.put("flag", false);
		}
		
		return retMsg;
	}
	
}
